package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("SetmealController")
@RequestMapping("/admin/setmeal")
@Api(tags="套餐相关接口")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService dishSetmeal;


    @PostMapping
    @ApiOperation("新增套餐接口")
    @CacheEvict(cacheNames="setmealCache",key="#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐:{}",setmealDTO);
        dishSetmeal.save(setmealDTO);
        return Result.success();
    }
    @GetMapping("/page")
    @ApiOperation("分页查询接口")
    public Result page(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("SetmealPage:{}",setmealPageQueryDTO);
        PageResult pageResult = dishSetmeal.page(setmealPageQueryDTO);
        return Result.success(pageResult);
    }
    @PostMapping("/status/{status}")
    @ApiOperation("状态改变接口")
    @CacheEvict(cacheNames="setmealCache",allEntries = true)
    public Result changestatus(@PathVariable("status") long status,long id){
        dishSetmeal.changestatus(status,id);
        return Result.success();
    }
    @GetMapping("/{id}")
    @ApiOperation("查询接口")
    public Result<SetmealVO> getById(@PathVariable Long id){
        log.info("id:{}",id);
        SetmealVO setmealVO = dishSetmeal.getById(id);
        return Result.success(setmealVO);
    }
    @PutMapping
    @ApiOperation("修改接口")
    @CacheEvict(cacheNames="setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        log.info("SetmealDTO:{}",setmealDTO);
        dishSetmeal.update(setmealDTO);
        return Result.success();
    }
    @DeleteMapping
    @ApiOperation("删除接口")
    @CacheEvict(cacheNames="setmealCache",allEntries = true)
    public Result delete(@RequestParam List<Long> ids){
        log.info("ids:{}",ids);
        dishSetmeal.deleteBySetmealId(ids);
        return Result.success();
    }
}
